package com.seqb.utils;

import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2020/4/30
 */
public class AesUtils {

    //偏移量
    public static final String VIPARA = "f%Z4F+qtFh624970";
    //编码方式
    public static final String CODE_TYPE = "UTF-8";
    //填充类型
    public static final String AES_TYPE_128 = "AES/ECB/PKCS7Padding";
    public static final String AES_TYPE_256 = "AES/CBC/PKCS7Padding";
    //私钥,AES固定格式为128/192/256 bits.即：16/24/32bytes
    private static final String AES_KEY_128 = "46cc793c53dc451b";
    private static final String AES_KEY_256 = "AG+BwcnekYZy$9f7X#b2zdB93brfFMmz";

    /**
     * 256加密带偏移量
     */
    public static String encryptPy(String cleartext) {
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(AES_KEY_256.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE_256);
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte[] encryptedData = cipher.doFinal(cleartext.getBytes(CODE_TYPE));
            return new BASE64Encoder().encode(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 256解密带偏移量
     */
    public static String decryptPy(String encrypted) {
        try {
            byte[] byteMi = new BASE64Decoder().decodeBuffer(encrypted);
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(AES_KEY_256.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE_256);
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, CODE_TYPE);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 128加密不带偏移量
     */
    public static String encrypt(String cleartext) {
        try {
            SecretKeySpec sKey = new SecretKeySpec(AES_KEY_128.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE_128);
            cipher.init(Cipher.ENCRYPT_MODE, sKey);
            byte[] decrypted = cipher.doFinal(cleartext.getBytes(CODE_TYPE));
            return Base64.encodeBase64String(decrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 128解密不带偏移量
     */
    public static String decrypt(String content) {
        try {
            byte[] sourceBytes = Base64.decodeBase64(content);
            Cipher cipher = Cipher.getInstance(AES_TYPE_128);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_KEY_128.getBytes(), "AES"));
            byte[] decoded = cipher.doFinal(sourceBytes);
            return new String(decoded, CODE_TYPE);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 测试启动类
     *
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String content = "/gNtrF5hsqYAzbmCb8IoIT2+7e7OzU8DM/8OrugPAiTFrrc2KCb0oerJU+EUcooFwGE1YJm3IjhVWorsCv0nNhh9HW6AeqvvxjfwJA7kDPMcKx9K3WgBR9e2L9JYh3eU11SCs/hbLZ5SQ+VCYb3cCG6Lwig6DCW/2f2DnfTmK3Q=";
        System.out.println(decrypt(content));
    }
}
